﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command.MainCommand;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander.MainHander
{
    public class DelMainCommandHander : IRequestHandler<DelMainCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Main> baseRequsitory;

        public DelMainCommandHander(IMapper mapper, IBaseRequsitory<Main> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelMainCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var list = baseRequsitory.GetAllAsync().Where(x=>x.MainId==request.MainId).FirstOrDefault();

            if (list!=null)
            {
                if (list.IsDelete == false)
                {
                    list.IsDelete = true;
                    baseRequsitory.UpdateAsync(list);
                }
                else
                {
                    list.IsDelete = false;
                    baseRequsitory.UpdateAsync(list);
                }
            }

            result.Code = ApiEnum.修改成功;
            result.Msg = ApiEnum.修改成功.ToString();
            return Task.FromResult(result);

        }
    }
}
